package com.ecable.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ecable.bean.PackagesTO;
import com.ecable.daoImpl.PackagesDaoImpl;
import com.ecable.util.UtilConstants;
import com.sun.org.apache.commons.beanutils.BeanUtils;

public class UpdatePackageAction extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 8739119206017332679L;


	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		String path="";
		RequestDispatcher rd=null;

		boolean flag=false;
		
	PackagesTO pack=new PackagesTO();
		
		pack.setPackid(Integer.parseInt(request.getParameter("Packid")));
		pack.setPackname(request.getParameter("Packname"));
		pack.setPackdiscription(request.getParameter("Packdiscription"));
		pack.setPackamount(Float.parseFloat(request.getParameter("Packamount")));
		pack.setPackstatus(request.getParameter("Packstatus"));
		System.out.println(request.getParameter("Packstatus"));
		/*Map map=request.getParameterMap();
	 try {
		BeanUtils.populate(pack, map);
	} catch (IllegalAccessException e1) {
		// TODO Auto-generated catch block
		e1.printStackTrace();
	} catch (InvocationTargetException e1) {
		// TODO Auto-generated catch block
		e1.printStackTrace();
		
	}*/
			try{
				flag=new PackagesDaoImpl().updatePackage(pack);
					
				if(flag){
					
					request.setAttribute("status", UtilConstants._UPDATE_PACKAGE);
				
					path=UtilConstants._UPDATE_SUCCESS;
					
					
				}
				else{
					
					request.setAttribute("status", UtilConstants._UPDATE_PACKAGE_FAILED);
					
					path=UtilConstants._UPDATE_SUCCESS;
					
				}
				
				
		}
			catch (Exception e) {
				e.printStackTrace();
				
				request.setAttribute("status", UtilConstants._INVALIED_ENTRY);
				
				path=UtilConstants._VIEW_PACKAGE;
				
			}
			
			
			 rd=request.getRequestDispatcher(path);
			
			rd.forward(request, response);
			
	}

}
